// pages/mark/mark.js
const { tingOptions, huOptions, transformOptions } = require('./config')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tingOptions: transformOptions(tingOptions),
    huOptions: transformOptions(huOptions),
    ting: '',
    hu: '',
    numHidden19: 0,
    numHiddenNot19: 0,
    numVisible19: 0,
    numVisibleNot19: 0,
    numHiddenGang19: 0,
    numHiddenGangNot19: 0,
    numVisibleGang19: 0,
    numVisibleGangNot19: 0,
    /** 是否为役牌雀头 */
    isYiPaiTou: false,
  },

  /**
   * 获取刻子的符数
   */
  getTripleMarks() {
    const { numHidden19, numHiddenNot19, numVisible19, numVisibleNot19 } = this.data;
    let marks = 0;
    // 幺九暗刻8符
    marks += numHidden19 * 8;
    // 非幺九暗刻4符
    marks += numHiddenNot19 * 4;
    // 幺九明刻4符
    marks += numVisible19 * 4;
    // 非幺九明刻2符
    marks += numVisibleNot19 * 2;
    return marks;
  },
  /**
   * 获取杠子的符数
   */
  getGangMarks() {
    const { numHiddenGang19, numHiddenGangNot19, numVisibleGang19, numVisibleGangNot19 } = this.data;
    let marks = 0;
    /** 幺九暗杠32符 */
    marks += numHiddenGang19 * 32;
    /** 非幺九暗杠16符 */
    marks += numHiddenGangNot19 * 16;
    /** 幺九明杠16符 */
    marks += numVisibleGang19 * 16;
    /** 非幺九明杠8符 */
    marks += numVisibleGangNot19 * 8;
    return marks;
  },
  /**
   * 获取其他符数
   */
  getOtherMarks() {
    const { ting, hu, isYiPaiTou } = this.data;
    let marks = 0;
    if (ting === '单骑' || ting === '坎张' || ting === '边张') {
      marks += 2;
    }
    if (hu === '自摸') {
      marks += 2;
    } else if (hu === '门清荣和') {
      marks += 10;
    }
    if (isYiPaiTou) {
      marks += 2;
    }
    return marks;
  },
  /**
   * 获取所有符数
   */
  handleSumbit() {
    const marks = this.getGangMarks() + this.getTripleMarks() + this.getOtherMarks()
    const result = Math.ceil(marks / 10) * 10 + 20;
    if (result > 110) {
      wx.showToast({
        title: '符数最多为110，请检查',
        icon: 'none',
      })
      return;
    }
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('link', result);
    wx.navigateBack();
  }
})